import {
  Controller,
  Get,
  Post,
  Req,
  Ip,
  Body,
  Param,
  Query,
} from '@nestjs/common';
import { Request } from 'express';

@Controller('cats')
export class CatController {
  @Get()
  findAll(@Req() request: Request, @Ip() ip: string, @Query() query: any): any {
    const requestData = {
      method: request.method,
      url: request.url,
      headers: request.headers,
      ip: ip,
      query: query,
    };
    //console.log('request', request); // 因为request对象内部有循环引用,JSON序列化时会报错,建议返回部分参数
    return requestData;
  }

  @Get(':thisone')
  findOne(@Query() query: any, @Param() qarams: any): any {
    console.log('qarams', qarams);
    const cat = {
      name: `cat_find_${query.id}`,
      age: `cat_age_${query.age}`,
    };
    return cat;
  }

  @Post('create')
  create(@Body() bodyData: any): unknown {
    return {
      name: `cat_${bodyData?.name}_${bodyData?.age}`,
      age: bodyData?.age,
    };
  }
}
